package com.eureka.asyn.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.eureka.asyn.service.MsgService;
import com.eureka.asyn.util.SmsUtil;
import com.eureka.baserocketmq.service.RocketMqService;
import com.eureka.bclass.base.BaseException;
import com.eureka.bclass.base.BaseMsgEntity;
import com.eureka.bclass.enums.MsgCategoryEnum;
import com.eureka.bclass.enums.RocketMqDelayLevelEnum;
import com.eureka.bclass.constant.RocketMqTopicConstant;
import com.eureka.bclass.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.concurrent.CompletableFuture;

/**
 * @Description: 短信服务实现类
 * @Author: lixing
 * @Date: 2021/6/6 12:36
 */
@Service
public class MsgServiceImpl implements MsgService {
    /**
     * 阿里云 OSS 云文件存储相关配置
     */
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.regionId}")
    private String regionId;
    @Value("${aliyun.sms.signName}")
    private String signName;
    @Value("${aliyun.sms.templateCodeForUserLogin}")
    private String templateCodeForUserLogin;
    @Value("${aliyun.sms.templateCodeForUpdatePwd}")
    private String templateCodeForUpdatePwd;

    @Resource
    private RocketMqService<BaseMsgEntity> rocketMqService;

    /**
     * @Description TODO 用户登录
     * @Author lixing
     * @Date 2019/3/30 15:51
     * @Param [
     *  category: 短信类型，详见 MsgCategoryEnum 枚举
     *  receiver: 接收者的手机号码
     * ]
     */
    @Override
    public CompletableFuture<Boolean> userLogin(BaseMsgEntity baseMsgEntity) {
        if (MsgCategoryEnum.USER_LOGIN.getCategory().equals(baseMsgEntity.getCategory())) {
            String code = StrUtil.genRandomStr("mix", 4);
            baseMsgEntity.setCode(code);
            baseMsgEntity.setText(MessageFormat.format(MsgCategoryEnum.USER_LOGIN.getDescribe(), code));
        } else {
            throw new BaseException("短信类型非法");
        }
        // 按照短信模板构建参数，格式：尊敬的用户您好！您本次登录的验证码为：${code}，请不要告诉别人!
        JSONObject param = new JSONObject();
        param.put("code", baseMsgEntity.getCode());
        // TODO 发送短信
        Boolean flag = SmsUtil.sendSms( regionId, accessKeyId, accessKeySecret, signName, templateCodeForUserLogin, baseMsgEntity.getReceiver(), param);
        // TODO RocketMq异步处理
        rocketMqService.sendAsyncMsg(RocketMqTopicConstant.LOG_TOPIC_MSG, baseMsgEntity, 10000, RocketMqDelayLevelEnum.SECONDS_ONE.getLevel());
        return CompletableFuture.completedFuture(flag);
    }

    /**
     * @Description TODO 修改密码
     * @Author lixing
     * @Date 2019/3/30 15:51
     * @Param [
     *  category: 短信类型，详见 MsgCategoryEnum 枚举
     *  receiver: 接收者的手机号码
     * ]
     */
    @Override
    public CompletableFuture<Boolean> updatePwd(BaseMsgEntity baseMsgEntity) {
        if (MsgCategoryEnum.USER_UPDATE_PWD.getCategory().equals(baseMsgEntity.getCategory())) {
            String code = StrUtil.genRandomStr("mix", 4);
            baseMsgEntity.setCode(code);
            baseMsgEntity.setText(MessageFormat.format(MsgCategoryEnum.USER_UPDATE_PWD.getDescribe(), code));
        } else {
            throw new BaseException("短信类型非法");
        }
        // 按照短信模板构建参数，格式：尊敬的用户您好！您本次修改密码的验证码为：${code}，请不要告诉别人!
        JSONObject param = new JSONObject();
        param.put("code", baseMsgEntity.getCode());
        // TODO 发送短信
        Boolean flag = SmsUtil.sendSms( regionId, accessKeyId, accessKeySecret, signName, templateCodeForUpdatePwd, baseMsgEntity.getReceiver(), param);
        // TODO RocketMq异步处理
        rocketMqService.sendAsyncMsg(RocketMqTopicConstant.LOG_TOPIC_MSG, baseMsgEntity, 10000, RocketMqDelayLevelEnum.SECONDS_ONE.getLevel());
        return CompletableFuture.completedFuture(flag);
    }
}
